package com.smart.lx.prototype;

import java.io.*;
import java.util.List;

/**
 * 功能描述: <br>
 * 〈原型模式〉
 *
 * @since: 1.0.0
 * @Author:abc
 * @Date: 2019/4/5
 */
public class DemoPrototype implements Cloneable, Serializable {

    private String name;
    private int age;
    private List<String> hobby;


    //人工克隆
    public DemoPrototype clone(DemoPrototype target){
        DemoPrototype modle = new DemoPrototype();
        modle.setAge(target.getAge());
        modle.setName(target.getName());
        modle.setHobby(target.getHobby()); //浅克隆
        return modle;
    }

    //cloneable 浅克隆
    public DemoPrototype cloneV2() throws Exception{
        return (DemoPrototype) this.clone();
    }

    //深克隆;  引用数据类型(包括父类)必须实现Serializeable才能被深克隆;
    public DemoPrototype cloneV3(DemoPrototype target) throws Exception{

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        oos.writeObject(target);
        oos.flush();
        oos.close();
        ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
        ObjectInputStream ois = new ObjectInputStream(bis);
        DemoPrototype copy = (DemoPrototype)ois.readObject();

        ois.close();
        return copy;
    }

    public List<String> getHobby() {
        return hobby;
    }
    public void setHobby(List<String> hobby) {
        this.hobby = hobby;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }


}
